package de.preiscrawler.parser.amazon;

import java.util.List;

import org.apache.log4j.Logger;

import net.htmlparser.jericho.Element;
import de.preisrobot.entity.Artikel;

public class AmazonListTemplateParserV1
{
   private static Logger log = Logger.getLogger(AmazonListTemplateParserV1.class);
   
   private Element element;

   public AmazonListTemplateParserV1(Element element)
   {
     this.element = element;
   }

   public Artikel parse() throws ElementNotFoundException
   {
      Artikel a = new Artikel();
      getDescription(a);
      return a;
   }

   private void getDescription(Artikel a) throws ElementNotFoundException
   {
     List<Element> allElementsByClass = element.getAllElementsByClass("data");
     
     if(allElementsByClass.isEmpty())
        throw new ElementNotFoundException("div class=data");
     
     Element divTitle = allElementsByClass.get(0);
     List<Element> ahref = divTitle.getAllElements("a");
     
     if(ahref.isEmpty())
        throw new ElementNotFoundException("a href=");
     
     Element element2 = ahref.get(0);
     
     String atrikelLink = element2.getAttributeValue("href");
     String artikelDesc = element2.getTextExtractor().toString();
     
 
     a.setArtikelLink(atrikelLink);
     a.setDesc(artikelDesc);
     
   }

}
